<?php

	$this->Html->script(array('somente-numeros'), false);
	
	echo '<fieldset><legend>Dados</legend>';
	
	echo '<br/>';
	
	echo 'Aluno(a): <b>'. $this->request->data['Aluno']['nome'].'<br/><br/>'.'</b>';
	
	echo $this->Form->create('Matricula', array('onsubmit' => 'return valida();'));
	echo $this->Form->input('id');
	echo $this->Form->input('aluno_id',array('type' => 'hidden'));
	echo $this->Form->input('situacao_academica',array('type' => 'hidden', 'value'=> 2));
	echo $this->Form->input('responsavel_fin_id', array('type' => 'hidden'));
	echo $this->Form->input('responsavel_ped_id', array('type' => 'hidden'));
	echo $this->Form->input('ano', array('type' => 'text', 'label' => __('Ano'), 'class' => 'tamanho5', 'maxlength' => 4, 'onKeyPress' => 'return SomenteNumero(event)'));
	echo $this->Form->input('tipo_serie_id',array('type' => 'select','class'=>'tamanho50', 'options'=>$series, 'label' => __('Série')));
	echo $this->Form->input('turma_id',array('type' => 'select', 'class' => 'tamanho20', 'options' => $turmas, 'label' => __('Turma')));
	echo '<div style="clear:both;"></div>'.'<br/>';
	echo $this->Form->input('responsavel_financeiro_nome', array('type' => 'text', 'label' => __('Responsável Financeiro'), 'class' => 'tamanho50', 'value' => $nome_responsavel_financeiro, 'div' => array('class' => 'required')));
	echo '<div style="clear:both;"></div>'.'<br/>';
	echo $this->Form->input('responsavel_pedagogico_nome', array('type' => 'text', 'label' => __('Responsável Pedagógico'), 'class' => 'tamanho50', 'value' => $nome_responsavel_pedagogico, 'div' => array('class' => 'required')));
	echo '<div class="botoes_form">';
	echo $this->Form->button('Salvar', array('type' => 'submit'));
	echo $this->Form->button('Voltar', array('type'=>'button', 'onClick' => "javascript:location='{$this->request->base}/{$this->request->controller}/finalizar_matricula'"));
	echo '</div>'; 
	echo $this->Form->end();

	echo '</fieldset>';
?>

<script>

	function valida() {

		if ($('#MatriculaTurmaId').val() == 0) {
			alert('Turma deve ser selecionada!');
			return false;
		}

		return true;
	}

	$("#MatriculaResponsavelFinanceiroNome").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: '<?php echo $this->Html->url(array('controller' => 'responsaveis', 'action' => 'pesquisar')); ?>',
				dataType: "jsonp",
				data: {
					nome: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							id: item.id,
							value: item.value
						}
					}));
				}
			});
		},
		
		minLength: 3,
		
		select: function( event, ui ) {
			$("#MatriculaResponsavelFinId").val(ui.item.id);
			$("#MatriculaResponsavelFinanceiroNome").val(ui.item.value);

		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});

	$("#MatriculaResponsavelPedagogicoNome").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: '<?php echo $this->Html->url(array('controller' => 'responsaveis', 'action' => 'pesquisar')); ?>',
				dataType: "jsonp",
				data: {
					nome: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							id: item.id,
							value: item.value
						}
					}));
				}
			});
		},
		
		minLength: 3,
		
		select: function( event, ui ) {
			$("#MatriculaResponsavelPedId").val(ui.item.id);
			$("#MatriculaResponsavelPedagogicoNome").val(ui.item.value);

		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});	

	$('#MatriculaTipoSerieId').change(function(){

		var ano = $('#MatriculaAno').val();
		var tipo_serie_id = $(this).val();
		
		$.ajax({
			type: 'POST',
			url: '<?php echo $this->request->base; ?>/enturmacoes/listar_turmas/' + ano + '/' + tipo_serie_id,
			async: true,
			beforeSend : function() {
				$('#MatriculaTurmaId').html('<option>Carregando...</option>');
			},
			success: function(retorno){
				$('#MatriculaTurmaId').html(retorno);
			}
			
		});			

	});
	
</script>